package com.xkdx.guangguang.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.xkdx.caipiao.R;
import com.xkdx.guangguang.HomeActivity;
import com.xkdx.guangguang.fragment.home.HomeFragment;
import com.xkdx.guangguang.fragment.home.HomeModule.HomeInfo;
import com.xkdx.guangguang.fragment.home.homedetail.HomeDetailFragment;
import com.xkdx.guangguang.wiget.CustomListView;

public class HomeImageAdapter extends BaseAdapter {

	List<HomeInfo> list;
	private LayoutInflater inflater;
	CustomListView listView;
	int height = 0;
	HomeFragment homeFragment;
	private Context context;
	ImageLoader imageLoader = ImageLoader.getInstance();
	DisplayImageOptions options;

	public HomeImageAdapter(Context context, List<HomeInfo> list, CustomListView listView) {

		this.context = context;
		this.listView = listView;
		this.list = list;
		this.inflater = ((LayoutInflater) context.getSystemService("layout_inflater"));
		FragmentManager fm = ((HomeActivity) context).getSupportFragmentManager();

		homeFragment = (HomeFragment) fm.findFragmentByTag("homeFragment");
		options = new DisplayImageOptions.Builder().showStubImage(R.drawable.empty_photo)
				.showImageForEmptyUri(R.drawable.empty_photo).showImageOnFail(R.drawable.empty_photo)
				.resetViewBeforeLoading().cacheOnDisc().imageScaleType(ImageScaleType.EXACTLY)
				.bitmapConfig(Bitmap.Config.RGB_565).build();

	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.home_image, null);
			viewHolder = new ViewHolder();
			viewHolder.im = (ImageView) convertView.findViewById(R.id.image);

			viewHolder.ll_homeimage = (LinearLayout) convertView.findViewById(R.id.ll_homeimage);

			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		String url = list.get(position).getPictureOriUrl();
		viewHolder.im.setTag(url);
		imageLoader.displayImage(url, viewHolder.im, options);

		// 图片点击事件
		viewHolder.ll_homeimage.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					((CustomListView) listView).startTouch();
				}
				return false;
			}
		});

		viewHolder.ll_homeimage.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				FragmentTransaction ft = homeFragment.getFragmentManager().beginTransaction();
				HomeDetailFragment homeDetailFragment = new HomeDetailFragment();
				ft.replace(R.id.fragment, homeDetailFragment);
				Bundle b = new Bundle();
				homeDetailFragment.setArguments(b);
				b.putString("infoID", list.get(position).getInfoID());
				ft.addToBackStack(null);
				ft.commit();
			}
		});


		return convertView;
	}

	private static class ViewHolder {
		LinearLayout ll_homeimage;
		ImageView im;
		LinearLayout ll;
	}

}
